/*
 * @Author: Star linxinh@qq.com
 * @Date: 2023-04-04 09:35:06
 * @LastEditors: Star linxinh@qq.com
 * @LastEditTime: 2023-04-04 10:58:37
 * @FilePath: \LOG\module_debug.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef _MODULE_DEBUG_H_
#define _MODULE_DEBUG_H_

#ifndef SUPPORT_ANSI
#define SUPPORT_ANSI 1
#endif

#define COL(x) "\033[;" #x "m"
#define RED COL(31)
#define GREEN COL(32)
#define YELLOW COL(33)
#define BLUE COL(34)
#define MAGENTA COL(35)
#define CYAN COL(36)
#define WHITE COL(0)
#define GRAY "\033[0m"

enum
{
    LOG_LEVEL_NONE = 0,
    LOG_LEVEL_DEBUG,
    LOG_LEVEL_INFO,
    LOG_LEVEL_WARN,
    LOG_LEVEL_ERROR,
    LOG_LEVEL_FATAL
};

void log_fun(int level, const char *opt, const char *tag, int line, const char *func, const char *fmt, ...);

#define LOG_DBG(tag, fmt, ...)                                                      \
    do                                                                              \
    {                                                                               \
        if (SUPPORT_ANSI)                                                           \
            printf("\033[1;32m");                                                   \
        log_fun(LOG_LEVEL_DEBUG, "D", tag, __LINE__, __func__, fmt, ##__VA_ARGS__); \
        if (SUPPORT_ANSI)                                                           \
            printf("\033[0m");                                                      \
    } while (0)

#define LOG_INFO(tag, fmt, ...)                                                    \
    do                                                                             \
    {                                                                              \
        if (SUPPORT_ANSI)                                                          \
            printf("\033[1;34m");                                                  \
        log_fun(LOG_LEVEL_INFO, "I", tag, __LINE__, __func__, fmt, ##__VA_ARGS__); \
        if (SUPPORT_ANSI)                                                          \
            printf("\033[0m");                                                     \
    } while (0)

#define LOG_WARN(tag, fmt, ...)                                                    \
    do                                                                             \
    {                                                                              \
        if (SUPPORT_ANSI)                                                          \
            printf("\033[1;33m");                                                  \
        log_fun(LOG_LEVEL_WARN, "W", tag, __LINE__, __func__, fmt, ##__VA_ARGS__); \
        if (SUPPORT_ANSI)                                                          \
            printf("\033[0m");                                                     \
    } while (0)

#define LOG_ERR(tag, fmt, ...)                                                      \
    do                                                                              \
    {                                                                               \
        if (SUPPORT_ANSI)                                                           \
            printf("\033[1;31m");                                                   \
        log_fun(LOG_LEVEL_ERROR, "E", tag, __LINE__, __func__, fmt, ##__VA_ARGS__); \
        if (SUPPORT_ANSI)                                                           \
            printf("\033[0m");                                                      \
    } while (0)

#define LOG_FATAL(tag, fmt, ...)                                                    \
    do                                                                              \
    {                                                                               \
        if (SUPPORT_ANSI)                                                           \
            printf("\033[1;35m");                                                   \
        log_fun(LOG_LEVEL_FATAL, "F", tag, __LINE__, __func__, fmt, ##__VA_ARGS__); \
        if (SUPPORT_ANSI)                                                           \
            printf("\033[0m");                                                      \
    } while (0)

#define LOG_MY(col, level, tag, fmt, ...)                               \
    do                                                                  \
    {                                                                   \
        if (SUPPORT_ANSI)                                               \
            printf(col);                                                \
        log_fun(0, level, tag, __LINE__, __func__, fmt, ##__VA_ARGS__); \
        if (SUPPORT_ANSI)                                               \
            printf(GRAY);                                               \
    } while (0)

void ModuleDebugInit(int level);

#endif
